package alp.starcode.sanping.information.controller;

import alp.starcode.common.base.annotation.operate.Add;
import alp.starcode.common.base.annotation.operate.Delete;
import alp.starcode.common.base.annotation.operate.Update;
import alp.starcode.common.mybatis.page.Pagination;
import alp.starcode.common.mybatis.sort.Sorter;
import alp.starcode.common.web.result.Result;
import alp.starcode.sanping.common.BizConstant;
import alp.starcode.sanping.crawler.impl.CnGxbServiceImpl;
import alp.starcode.sanping.crawler.impl.HeBeiGxbServiceImpl;
import alp.starcode.sanping.dict.vo.DictDataVO;
import alp.starcode.sanping.framework.base.BaseController;
import alp.starcode.sanping.framework.database.mariadb.mybatis.entity.Information;
import alp.starcode.sanping.information.model.dto.InformationDTO;
import alp.starcode.sanping.information.model.vo.InformationVO;
import alp.starcode.sanping.information.service.InformationService;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * TODO
 *
 * @author wujie
 * @date 2024/3/14 17:32
 */
@Api(tags = "信息资讯")
@RestController
@RequestMapping("information")
@Validated
public class InformationController extends BaseController {

    @Resource
    private InformationService informationService;

    @GetMapping("pageList.do")
    @ApiOperation("信息资讯-分页查询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "infoType", value = "通知类型", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "region", value = "地区", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "industryType", value = "行业类型", dataType = "String", paramType = "query", required = false),
    })
    @ApiOperationSupport(order = 10)
    public Result<Pagination<InformationVO>> pageList(Pagination<Information> pagination, String infoType, String region, String industryType, Sorter sorters) {
        return Result.success(informationService.pageList(pagination, infoType, region, industryType, sorters));
    }

    @GetMapping("getById.do")
    @ApiOperation("信息资讯-根据ID查询信息")
    @ApiImplicitParam(name = "id", value = "流水号", dataType = "String", paramType = "query", required = false)
    @ApiOperationSupport(order = 20)
    public Result<InformationVO> getById(@NotEmpty(message = "流水号不能为空") String id) {
        return Result.success(informationService.getById(id));
    }

    @PostMapping("add.do")
    @ApiOperation("信息资讯-添加数据")
    @ApiImplicitParams({})
    @ApiOperationSupport(order = 30)
    public Result<Object> add(@Validated(Add.class) InformationDTO entityDTO) {
        informationService.add(entityDTO);
        return Result.success();
    }

    @PostMapping("delete.do")
    @ApiOperation("信息资讯-删除数据")
    @ApiImplicitParam(value = "流水号", name = "id", defaultValue = "1", dataType = "String", paramType = "query", required = true)
    @ApiOperationSupport(order = 40)
    public Result<Object> delete(@ApiIgnore @Validated(Delete.class) InformationDTO entityDTO) {
        informationService.delete(entityDTO);
        return Result.success();
    }

    @PostMapping("update.do")
    @ApiOperation("信息资讯-修改数据")
    @ApiImplicitParams({})
    @ApiOperationSupport(order = 50)
    public Result<Object> update(@Validated(Update.class) InformationDTO entityDTO) {
        informationService.update(entityDTO);
        return Result.success();
    }

    @GetMapping("getDataCn.do")
    @ApiOperation("信息资讯-测试数据获取入库-全国")
    @ApiOperationSupport(order = 20)
    public Result<InformationVO> getDataCn() {
        CnGxbServiceImpl.run();
        informationService.addBatch(BizConstant.SP_INFORMATION_LIST);
        return Result.success();
    }

    @GetMapping("getDataHeBei.do")
    @ApiOperation("信息资讯-测试数据获取入库-河北")
    @ApiOperationSupport(order = 20)
    public Result<InformationVO> getDataHeBei() {
        HeBeiGxbServiceImpl.run();
        informationService.addBatch(BizConstant.SP_INFORMATION_LIST);
        return Result.success();
    }

    @GetMapping("getIndustryType.do")
    @ApiOperation("信息资讯-获取行业类型")
    @ApiOperationSupport(order = 20)
    public Result<List<DictDataVO>> getIndustryType() {
        List<Information> industryTypeList = informationService.getIndustryType();
        List<DictDataVO> dictDataVOList = new ArrayList<>();
        industryTypeList.forEach((obj) -> {
            DictDataVO dictDataVO = new DictDataVO();
            dictDataVO.setDictLabel(obj.getIndustryType());
            dictDataVO.setDictName(obj.getIndustryType());
            dictDataVO.setDictValue(obj.getIndustryType());
            dictDataVOList.add(dictDataVO);
        });
        return Result.success(dictDataVOList);
    }
}
